/***********************************************************
		Group			:		G2
		Project         :       Big Table
		File Name       :       Query.java
		Author			:		Preetika Tyagi
		Purpose         :       To process user queries
		Change History  :
 ***********************************************************/

package BigT;

import global.GlobalConst;
import global.MID;
import heap.FieldNumberOutOfBoundException;

import java.io.IOException;

public class Query {
	public void queryResult(String bigTName, int type, int orderType,
			String rowFilter, String columnFilter, String valueFilter,
			int numBuf) {
		bigt bigtObj = new bigt();
		if (numBuf == 0) {
			numBuf = GlobalConst.NUMBUF;
		}
		Stream stream = bigtObj.openStream(bigtObj, bigTName, type, orderType,
				rowFilter, columnFilter, valueFilter, numBuf);
		MID mid1 = new MID();
		Map map1 = null;
		boolean done = false;
		int counter = 0;
		System.out
				.println("############################################ QUERY RESULT ###################################################");
		System.out.println("ROW" + "\t\t\t\t" + "COLUMN" + "\t\t\t\t"
				+ "TIMESTAMP" + "\t\t\t\t" + "VALUE");
		System.out
				.println("#############################################################################################################");
		int counterMaps = 0;
		while (done != true) {
			map1 = stream.getNext(mid1);
			if (map1 == null) {
				done = true;
				break;
			}
			String rowLabel = "";
			String colLabel = "";
			int ts = 0;
			String value = "";
			try {
				rowLabel = map1.getRowLabel();

				if (rowFilter != null & !rowFilter.equalsIgnoreCase("")
						& !rowLabel.equalsIgnoreCase(rowFilter)) {
					continue;
				}
				colLabel = map1.getColumnLabel();
				if (columnFilter != null & !columnFilter.equalsIgnoreCase("")
						& !colLabel.equalsIgnoreCase(columnFilter)) {
					continue;
				}
				ts = map1.getTimeStamp();
				value = map1.getValue();
				if (valueFilter != null & !valueFilter.equalsIgnoreCase("")
						& !value.equalsIgnoreCase(valueFilter)) {
					continue;
				}
				if (rowLabel.equalsIgnoreCase("Dominica")) {
					counter++;
				}
				map1.print();
				counterMaps++;
			} catch (FieldNumberOutOfBoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println("No of Dominics Maps: " + counter);
		System.out.println("No of Maps fetched: " + counterMaps);
		System.out.println("");
		System.out
				.println("#############################################################################################################");

		System.out.println("DISK READ COUNTER: "
				+ GlobalConst.pCounter.rcounter);
		System.out.println("DISK WRITE COUNTER: "
				+ GlobalConst.pCounter.wcounter);

		System.out
				.println("#############################################################################################################");

	}
}